package com.hy.common.excel;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.hy.common.utils.CommonUtils;
import com.hy.common.utils.StateUtil;
import com.hy.entity.dto.UserSearchDTO;
import com.hy.entity.vo.HyUserVO;
import com.hy.entity.vo.UserExportVO;
import com.hy.service.SysUserService;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.*;

@Component
public class UserExcelExport extends AbstractExcelExport<UserExportVO>{


    @Resource
    private SysUserService userService;
    private  static Integer totalCount;

    public void exportWithSmallData(HttpServletResponse response, String fileName, UserSearchDTO userSearchDTO){
        List<String> head = new ArrayList<String>();
        head.add("userId");
        head.add("department");
        List<HyUserVO> list = userService.findList(BeanUtil.beanToMap(userSearchDTO));
        totalCount=list.size();
        this.exportWithBigData(response, fileName, null,BeanUtil.beanToMap(userSearchDTO), UserExportVO.class);
    }
    @Override
    protected Integer dataTotalCount(Map<String, Object> queryCondition) {
        return  totalCount;
    }

    @Override
    protected Integer eachSheetTotalCount() {//每一个sheet存放的数据总数
        return 500000;
    }

    @Override
    protected Integer eachTimesWriteSheetTotalCount() {//每次写入sheet的总数
        return 5000;
    }

    @Override
    protected void buildDataList(List<UserExportVO> resultList, Map<String, Object> queryCondition, Integer pageNo, Integer pageSize) {

        List<HyUserVO> entityDoList = userService.listPage(queryCondition, pageNo, pageSize);
        if(CollUtil.isNotEmpty(entityDoList)){
            entityDoList.forEach(item -> {
                StringBuilder auth=new StringBuilder("");

                item.getRoles().forEach(e-> auth.append(e).append(","));
                if (auth.length()>0) auth.deleteCharAt(auth.length()-1);
                resultList.add(UserExportVO.builder()
                        .userId(item.getUserId())
                        .phone(item.getPhone())
                        .auth(auth.toString())
                        .sex(StateUtil.getSex(item.getSex().intValue()))
                        .createDate(item.getCreateDate())
                        .updateDate(item.getUpdateDate())
                        .username(item.getUsername())
                        .unit(item.getUnit()+"("+item.getUnitId()+")")
                        .department(item.getDepartment()+"("+item.getDepartmentId()+")")
                        .password(item.getPassword())
                        .build()

                );
//                resultList.add(Arrays.asList(String.valueOf(item.getId()),String.valueOf(item.getEmpno()),String.valueOf(item.getEname()),item.getJob(),String.valueOf(item.getMgr()),String.valueOf(item.getDeptno())));
            });
        }

    }
}
